﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using MyHotel_Common;
using System.Data.SqlClient;

namespace MyHotel_DAO
{
    public class MatHangDAO : DAO
    {
        protected override object createObjectFromDatarow(System.Data.DataRow row)
        {
            MatHang matHang = new MatHang();
            matHang.GiaBanHienHanh = int.Parse(row["GiaBanHienHanh"].ToString());
            matHang.GiaNhapHienHanh = int.Parse(row["GiaNhapHienHanh"].ToString());
            matHang.IsActive = bool.Parse(row["TinhTrang"].ToString());
            matHang.MaLoai = int.Parse(row["MaLoai"].ToString());
            matHang.MaMH = int.Parse(row["MaMH"].ToString());
            matHang.SoLuong = int.Parse(row["SoLuong"].ToString());
            matHang.TenMH = row["Ten"].ToString();

            return matHang;
        }

        public List<MatHang> getListMatHang()
        {

            string nameProc = "SP_GETLIST_MATHANG";
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null);

            return getListFromDataTable<MatHang>(dt); 
        }

        public bool themMatHang(MatHang matHang)
        {
            string nameProc = "SP_THEM_MATHANG";
            SqlParameter[] para = new SqlParameter[4];
            para[0] = new SqlParameter("@Ten", matHang.TenMH);
            para[1] = new SqlParameter("@MaLoai", matHang.MaLoai);
            para[2] = new SqlParameter("@GiaBanHienHanh", matHang.GiaBanHienHanh);
            para[3] = new SqlParameter("@GiaNhapHienHanh", matHang.GiaNhapHienHanh);

            return (DataProvider.executeStoreProcedureNonQuery(nameProc, para) > 0); 
        }

        public bool xoaMatHang(int maMH)
        {
            string nameProc = "SP_XOA_MATHANG";
            
            return (DataProvider.executeStoreProcedureNonQuery(nameProc, new SqlParameter("@MaMH", maMH)) > 0);
        }

        public bool suaMatHang(MatHang matHang)
        {
            String nameProc = "SP_SUA_MATHANG";
            SqlParameter[] para = new SqlParameter[5];
            para[0] = new SqlParameter("@Ten", matHang.TenMH);
            para[1] = new SqlParameter("@MaLoai", matHang.MaLoai);
            para[2] = new SqlParameter("@GiaBanHienHanh", matHang.GiaBanHienHanh);
            para[3] = new SqlParameter("@GiaNhapHienHanh", matHang.GiaNhapHienHanh);
            para[4] = new SqlParameter("@MAMH", matHang.MaMH);

            return (DataProvider.executeStoreProcedureNonQuery(nameProc, para) > 0); 
        }
    }
}
